package com.travelingsalesmen.post;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

public class Invoice implements Serializable {

    private String customerName;
    private Date date;
    private List<Item> items;
    private String paymentType;
    private boolean paymentSucceeded;
    private double total;
    private boolean inFile;


    public Invoice(Transaction t) {
        customerName = t.getCustomerName();
        date = t.getDate();
        items = t.getItems();
        paymentType = t.getPaymentType();
        paymentSucceeded = t.paymentWasSuccessful();
        total = t.getTotal();
    }

    public String getCustomerName() {
        return customerName;
    }

    public Date getDate() {
        return date;
    }

    public List<Item> getItems() {
        return items;
    }

    public boolean isPaymentSucceeded() {
        return paymentSucceeded;
    }

    public String getPaymentType() {
        return paymentType;
    }
    
    public boolean isInFile() {
        return inFile;
    }

    public void setInFile(boolean inFile) {
        this.inFile = inFile;
    }
   

    @Override
    /**
     * Return the invoice in XML format
     */
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("<sale store=\"Traveling Baker\">\n");
        sb.append("\t<date>");
        sb.append(date);
        sb.append("</date>\n");
        sb.append("\t<customer_name>");
        sb.append(customerName);
        sb.append("</customerName>\n");
        sb.append("\t<items>\n");
        for (Item item : getItems()) {
            sb.append("\t\t<item>\n");
            sb.append("\t\t\t<upc>");
            sb.append(item.getUpc());
            sb.append("</upc>\n");
            sb.append("\t\t\t<description>");
            sb.append(item.getDescription().trim());
            sb.append("</description>\n");
            sb.append("\t\t\t<quantity>");
            sb.append(item.getQty());
            sb.append("</quantity>\n");
            sb.append("\t\t\t<unit_price>");
            sb.append(String.format("$%.2f", item.getUnitCost()));
            sb.append("</unit_price>\n");
            sb.append("\t\t\t<subtotal>");
            sb.append(String.format("$%.2f", item.getSubtotal()));
            sb.append("</subtotal>\n");
            sb.append("\t\t</item>\n");
        }
        sb.append("\t</items>\n");
        sb.append("\t<total>");
        sb.append(String.format("$%.2f", total));
        sb.append("</total>\n");
        sb.append("\t<payment>\n");
        sb.append("\t\t<type>");
        sb.append(paymentType);
        sb.append("</type>\n");
        sb.append("\t\t<success>");
        sb.append(paymentSucceeded);
        sb.append("</success>\n");
        sb.append("\t</payment>\n");
        sb.append("</sale>");
        return sb.toString();
    }
}